﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Drawing;

namespace TerminationCall.Core {

    public class TileSetLoader {

        private BinaryReader _binaryReader;
        private TileSet _tileSet;

        public TileSet Load(string fileName) {

            _binaryReader = new BinaryReader(File.OpenRead(fileName));

            string tileSetImageName = _binaryReader.ReadString();
            string tileSetName = _binaryReader.ReadString();
            int horizontalTiles = _binaryReader.ReadInt32();
            int verticalTiles = _binaryReader.ReadInt32();

            _tileSet = new TileSet(tileSetImageName, horizontalTiles, verticalTiles);
            _tileSet.Name = tileSetName;

            for (int x = 0; x < horizontalTiles; x++) {

                for (int y = 0; y < verticalTiles; y++) {

                    Tile tile = _tileSet.Tiles[x, y];

                    tile.ParentTile = ReadTile();

                    _tileSet.GroupAutoborderTile(TileFields.TopTile, ReadTile(), tile);
                    _tileSet.GroupAutoborderTile(TileFields.RightTopTile, ReadTile(), tile);
                    _tileSet.GroupAutoborderTile(TileFields.LeftTopTile, ReadTile(), tile);
                    _tileSet.GroupAutoborderTile(TileFields.RightMiddleTile, ReadTile(), tile);
                    _tileSet.GroupAutoborderTile(TileFields.LeftMiddleTile, ReadTile(), tile);
                    _tileSet.GroupAutoborderTile(TileFields.BottomTile, ReadTile(), tile);
                    _tileSet.GroupAutoborderTile(TileFields.RightBottomTile, ReadTile(), tile);
                    _tileSet.GroupAutoborderTile(TileFields.LeftBottomTile, ReadTile(), tile);

                }

            }

            int tileObjectGroupsCount = _binaryReader.ReadInt32();

            for (int i = 0; i < tileObjectGroupsCount; i++) {

                Rectangle rectangle = new Rectangle(_binaryReader.ReadInt32(), _binaryReader.ReadInt32(), _binaryReader.ReadInt32(), _binaryReader.ReadInt32());
                string name = _binaryReader.ReadString();

                _tileSet.GroupObjectTile(rectangle, name);

            }

            _binaryReader.Close();

            return _tileSet;
        }

        private Tile ReadTile() {

            int numberX = _binaryReader.ReadInt32();
            int numberY = _binaryReader.ReadInt32();

            if (numberX == -1 && numberY == -1) {
                return null;
            }

            return _tileSet.Tiles[numberX, numberY];

        }

        public List<TileSet> LoadAll() {

            List<TileSet> tileSets = new List<TileSet>();

            Environment.CurrentDirectory = Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "TileSets");
            string[] files = Directory.GetFiles(Path.Combine(Path.GetDirectoryName(Application.ExecutablePath), "TileSets"));

            foreach (string file in files) {
                if (Path.GetExtension(file) == ".tis") {
                    tileSets.Add(Load(file));
                }
            }

            return tileSets;

        }

    }

}
